package com.jian.project.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.jian.project.dubbo.GatewayService;
import com.jian.project.mapper.DbInterfaceMapper;
import com.jian.project.mapper.TbUserInterfaceMapper;
import com.jian.project.model.dto.tbuserinterface.TbUserInterfaceInvokeRequest;
import com.jian.project.model.entity.DbInterface;
import com.jian.project.model.entity.TbUserInterface;
import com.jian.project.model.entity.User;
import com.jian.project.service.TbUserInterfaceService;
import com.jian.project.service.UserService;
import org.apache.dubbo.config.annotation.DubboService;

import javax.annotation.Resource;

@DubboService
public class DubboServiceImpl implements GatewayService {
    @Resource
    private UserService userService;
    @Resource
    private TbUserInterfaceService tbUserInterfaceService;
    @Resource
    private DbInterfaceMapper dbInterfaceMapper;
    @Resource
    private TbUserInterfaceMapper tbUserInterfaceMapper;

    @Override
    public User getSkByAk(String ak) {
        return userService.getSkByAk(ak);
    }

    @Override
    public Boolean invokeApi(TbUserInterfaceInvokeRequest request) {
        return tbUserInterfaceService.invokeApi(request);
    }

    @Override
    public DbInterface getDbInterfaceById(long id) {
        if (id <= 0)
            return null;
        return dbInterfaceMapper.selectById(id);
    }

    @Override
    public Integer remainingInvoke(Long userId, Long interfaceId) {
        if (userId == null || interfaceId == null)
            return -2;

        QueryWrapper<TbUserInterface> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("user_id",userId);
        queryWrapper.eq("interface_id",interfaceId);

        TbUserInterface tbUserInterface = tbUserInterfaceMapper.selectOne(queryWrapper);
        if (tbUserInterface == null){
            return -1;
        }
        return tbUserInterface.getLeftNum();
    }
}
